package com.oa.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Post;
import com.oa.entity.Power;
import com.oa.service.PostService;
import com.oa.service.PowerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class PostAction extends ActionSupport implements ModelDriven<Post> {
	private static final long serialVersionUID = 1L;

	// 通过spring 注入岗位服务类
	@Resource(name = "postService")
	private PostService postService;
	@Resource(name="powerService")
	private PowerService powerService;
	private Post post = new Post();
	
	//保存权限id
	private String[] powerIds;
	
	public String[] getPowerIds() {
		return powerIds;
	}
	public void setPowerIds(String[] powerIds) {
		this.powerIds = powerIds;
	}

	// 所有岗位的集合
	private List<Post> postList;

	public List<Post> getPostList() {
		return postList;
	}

	public void setPostList(List<Post> postList) {
		this.postList = postList;
	}

	// ===============================以下是业务操作=================

	// 显示 岗位
	public String list() {
		postList = postService.list();
		return "list";
	}

	// 添加 岗位
	public String add() {
		boolean bl = postService.postAdd(post);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 添加 页面
	public String addPage() {
		return "savePage";
	}

	// 修改 岗位
	public String update() {
		boolean bl = postService.postUpdate(post);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 修改页面
	public String updatePage() {
		Post post1 = postService.getById(post.getUnique_id());
		ActionContext.getContext().put("post1", post1);
		return "savePage";
	}

	// 删除 岗位
	public String delete() {
		boolean bl = postService.postDelete(post);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 设置权限
	public String setPower() {
		String id = ServletActionContext.getRequest().getParameter("unique_id");
		Post post1 = postService.getById(id);
		System.out.println("post1"+post1+"unique_id"+id);
		List<Power> powerList = powerService.getByIds(powerIds);
		post1.setPowerSet(new HashSet<Power>(powerList));
		System.out.println(">>>>>>>>"+post1.getPost_name()+post1.getPost_name()+post1.getPowerSet().size());
		boolean bl = postService.postUpdate(post1);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 设置权限页面
	public String setPowerPage() {
		Post post1 = postService.getById(post.getUnique_id());
		ActionContext.getContext().put("post1",post1);

		if (post1.getPowerSet() != null) {
			powerIds = new String[post1.getPowerSet().size()];
			int index = 0;
			for (Power power : post1.getPowerSet()) {
				powerIds[index++] = power.getUnique_id();
			}
		}

		// 准备数据 powerList
		List<Power> powerList = powerService.findAll();
		ActionContext.getContext().put("powerList", powerList); 

		return "setPowerPage";
	}
	
	

	// 重新获取模型的方法
	public Post getModel() {
		return post;
	}
}
